clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* data from FE_MO_June.do
use permco permno statpers medest* street_share_adj1 street_share_adj3 using $Work/FE_MO.dta, clear
gen datem=mofd(statpers)
format datem %tm

tsset permno datem
gen cpMo1=ln(F1.medest1)-ln(medest2)
gen cpQr1=ln(F3.medest1)-ln(medest4)

gstats winsor cp*1, cuts(1 99) by(datem) replace  
keep permco permno datem cp*
* ***********************************************************************************************
* data from read_ibes.do
merge 1:1 permno permco datem using  $Work/XSibes.dta, keepusing(permno permco datem statpers datem LTG) 
drop _merge

tsset permno datem
replace LTG=LTG/100
gen L1LTG=L12.LTG

gen dMFLTG=F1.LTG-LTG
gen dQFLTG=F3.LTG-LTG

forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
}
merge 1:1 permco permno datem using $Work/EBR_new_monthly
drop if _merge==1
drop _merge
d cp* FE* 

* data from Firms01_June.do
merge 1:1 permco permno datem using $Work/Firms01_June, keepusing(permco permno datem INV BOOK MOME SIZE SI10 MOME STG2 dp)
drop if _merge==2
drop _merge
drop SIZE
rename SI10 SIZE
gstats winsor FE* cp* STG2 *LTG, cuts(1 99) by(datem) replace
tempfile tmpstuff
save "`tmpstuff'", replace
* ************************************************
foreach group in BOOK {
use if `group'+qruf1<. using "`tmpstuff'", clear
generate portf=1+int((`group'-1)/2)
order *, sequential    
gcollapse (mean) ret_mo1-ret_mo60 ret1-ret60 LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2, by(datem portf)

gen  ret1Yr=1 
gen  ret2Yr=1 
gen  ret3Yr=1 
gen  ret4Yr=1 
gen  ret5Yr=1 	 
gen  Nret1Yr=1 
gen  Nret2Yr=1 
gen  Nret3Yr=1 
gen  Nret4Yr=1 
gen  Nret5Yr=1
forvalues x=1(1)60 {
	replace ret_mo`x'=ret_mo`x'-1
}	 
forvalues x=1(1)12 {
	replace  ret1Yr= ret1Yr*(1+ret`x')
	replace Nret1Yr=Nret1Yr*(1+ret_mo`x')
}
forvalues x=13(1)24 {
	replace   ret2Yr= ret2Yr*(1+ret`x')
	replace  Nret2Yr=Nret2Yr*(1+ret_mo`x')
}
forvalues x=25(1)36 {
	replace   ret3Yr= ret3Yr*(1+ret`x')
	replace  Nret3Yr=Nret3Yr*(1+ret_mo`x')
}
forvalues x=37(1)48 {
	replace   ret4Yr= ret4Yr*(1+ret`x')
	replace  Nret4Yr=Nret4Yr*(1+ret_mo`x')
}
forvalues x=49(1)60 {
	replace   ret5Yr= ret5Yr*(1+ret`x')
	replace  Nret5Yr=Nret5Yr*(1+ret_mo`x')
}
*********************
scalar alfa= 0.97749304
gen cumRetMo1=ln(1+ret1)
gen cumRetQr1=ln(1+ret1)+ln(1+ret2)+ln(1+ret3)
gen cumRetYr1=ln(ret1Yr)
gen cumRetYr3=ln(ret1Yr)+alfa^1*ln(ret2Yr)+alfa^2*ln(ret3Yr)
gen cumRetYr5=ln(ret1Yr)+alfa^1*ln(ret2Yr)+alfa^2*ln(ret3Yr)+alfa^3*ln(ret4Yr)+alfa^4*ln(ret5Yr)

gen cumNRetMo1=ln(1+ret_mo1)
gen cumNRetQr1=ln(1+ret_mo1)+ln(1+ret_mo2)+ln(1+ret_mo3)
gen cumNRetYr1=ln(Nret1Yr)
gen cumNRetYr3=ln(Nret1Yr)+alfa^1*ln(Nret2Yr)+alfa^2*ln(Nret3Yr)
gen cumNRetYr5=ln(Nret1Yr)+alfa^1*ln(Nret2Yr)+alfa^2*ln(Nret3Yr)+alfa^3*ln(Nret4Yr)+alfa^4*ln(Nret5Yr)
*********************
tempfile tmp
save "`tmp'", replace
su cumRetYr*
*********************
keep if portf==1 | portf==5 
foreach var of varlist cumRet* cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
replace `var'=-`var' if portf==5 & index("`PORTF'","INV")+index("`PORTF'","SIZE")==1
replace `var'=-`var' if portf==1 & index("`PORTF'","INV")+index("`PORTF'","SIZE")==0
}
foreach var of varlist cumRet* cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
	bys datem: egen n`var'=count(`var')
}
foreach var of varlist cumRet* cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
	replace `var'=. if n`var'<2
	drop n`var'
}
collapse (sum) cumRet* cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2,  by(datem)
foreach var of varlist cum* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
	replace `var'=. if `var'==0
}
su cumRetYr*
generate portf=6

append using `tmp'
sort portf datem
su cumRet* 
*********************
tsset portf datem
foreach var of varlist dp FE1-FE5 cp1-cp5 d*FLTG {
	su `var' if portf==1
	quietly:  bys portf: egen sd=sd(`var')
	quietly: replace  `var'=`var'/sd	
	drop sd
}

eststo clear
foreach p of numlist 6 {
gen FE=1
gen cp=cpMo1
gen dFLTG=dMFLTG
eststo new_`p'_1: quietly: ivreg2 cumNRetMo1 FE cp  	if portf==`p', bw(1)
drop FE cp dFLTG

gen FE=1
gen cp=cpQr1
gen dFLTG=dQFLTG
eststo new_`p'_2: quietly: ivreg2 cumNRetQr1 FE cp 	if portf==`p', bw(4)
drop FE cp dFLTG

gen FE=FE1
gen cp=cp1
gen dFLTG=d1FLTG
eststo new_`p'_3: quietly: ivreg2 cumNRetYr1 FE cp 	if portf==`p', bw(13)
drop FE cp dFLTG
gen FE=FE3
gen cp=cp3
gen dFLTG=d3FLTG
eststo new_`p'_4: quietly: ivreg2 cumNRetYr3 FE cp 	if portf==`p', bw(37)
drop FE cp dFLTG
gen FE=FE5
gen cp=cp5
gen dFLTG=d5FLTG
eststo new_`p'_5: quietly: ivreg2 cumNRetYr5 FE cp 	if portf==`p', bw(61)
drop FE cp dFLTG
}

local file "AppendixB5_June"
if "`group'"=="BOOK" {
esttab new*  using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a F) replace title(`group')
}
if "`group'"!="BOOK" {
esttab new*  using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a F) append  title(`group')
}
**********************************************
}
